package com.study.selection.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.study.selection.model.Classes;
import com.study.selection.model.College;
import com.study.selection.model.Major;
import com.study.selection.service.ClassesService;
import com.study.selection.service.CollegeService;
import com.study.selection.service.MajorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/class")
public class ClassController {
    // 注入Service
    @Autowired
    private MajorService majorService;

    @Autowired
    private CollegeService collegeService;

    @Autowired
    private ClassesService classesService;

    // 查询所有,绑定数据。
    @RequestMapping("/listAll")
    public String doList(@RequestParam(value = "page",defaultValue = "1") Integer page, Model model){
        //添加分页，page表示查询第几页
        PageHelper.startPage(page,5);
        List<Classes> list = classesService.findAll();
        PageInfo<Classes> info = new PageInfo<>(list,3);
        // 向页面返回封装后的数据
        model.addAttribute("info",info);
        return "/view/admin/class.jsp";
    }
    // 根据关键字查询,绑定数据。
    @RequestMapping("/search")
    public String doSearch(@RequestParam(value = "page",defaultValue = "1")Integer page,String key,Model model){
        PageHelper.startPage(page,5);
        List<Classes> list = classesService.findByKey(key);
        PageInfo<Classes> info = new PageInfo<>(list,3);
        model.addAttribute("info",info);
        model.addAttribute("key",key);
        return "/view/admin/class.jsp";
    }
   // 添加对象,接收表单参数,执行添加，重定向到listAll
    @RequestMapping("/add")
    public String doAdd(Classes classes){
        boolean result = classesService.addClass(classes);
        return "redirect:listAll";
    }
    //提前查询，并将查询数据转发
    @RequestMapping("/getAdd")
    public String doAdd(Classes classes,Model model){
        model.addAttribute("classes",classes);
        List<College> collegeList = collegeService.findAll();
        model.addAttribute("collegeList",collegeList);
        List<Major> majorList = majorService.findAll();
        model.addAttribute("majorList",majorList);
        return "/view/admin/add_class.jsp";
    }
    // 根据主键获取单个对象，转发到edit_college.jsp
    @RequestMapping("/getOne")
    public String doGetOne(int classesId,Model model){
        Classes classes= classesService.getByClassId(classesId);
        model.addAttribute("classes",classes);
        List<College> collegeList = collegeService.findAll();
        model.addAttribute("collegeList",collegeList);
        List<Major> majorList = majorService.findAll();
        model.addAttribute("majorList",majorList);
        return "/view/admin/edit_class.jsp";
    }

//    // 修改对象，接收表单参数，执行修改，重定向到listAll
    @RequestMapping("/update")
    public String doUpdate(Classes classes){
        boolean result=classesService.updateClass(classes);
            return "redirect:listAll";
    }
//  // 删除对象，接收请求参数id,执行删除，重定向到listAll
    @RequestMapping("/delete")
    public String doDelete(int classId){
        boolean result=classesService.deleteClass(classId);
        return "redirect:listAll";
    }
    @RequestMapping("/deleteItems")
    public String deleteItems(String[] chkIds) {
        for (String string : chkIds) {
            classesService.deleteClass(Integer.parseInt(string));
        }
        return "redirect:listAll";
    }

}



